package com.zw.mes.module.prdctionFlow.service;

import com.zw.mes.common.JsonResult;
import com.zw.mes.constant.Constant;
import com.zw.mes.entity.sys.Dept;
import com.zw.mes.module.prdctionFlow.mapper.TransQueryMapper;
import com.zw.mes.module.prdctionFlow.model.TransModel;
import com.zw.mes.module.prdctionFlow.model.TransOrderDataModel;
import com.zw.mes.module.system.mapper.DeptMapper;
import com.zw.mes.module.system.repository.DeptRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zw.mes.constant.Constant.STATUS_NORMAL;


/**
 * @Description:
 * @Author: China.sgl
 * @Date: 2019/12/6 08:42
 */
@Service
@SuppressWarnings("all")
public class TransQuerytService {

    @Autowired
    private TransQueryMapper transQueryMapper;


    /**
     * 按条件查询
     *
     * @param dept 条件
     * @return List
     */
    public List<TransModel> findTransByCondition(TransModel transModel) {
        return transQueryMapper.findTransByCondition(transModel);
    }

    /**
     * @methodName: loadTransOrderData
     * @description: 获取转序单数据
     * @param:
     * @return： com.zw.mes.common.JsonResult
     * @exception：
     * @author： 徐一贺 tel:18341916590
     * @designer： 陈欣
     * @leader： 陈欣 tel:18104194240
     * @date： 2021/4/22 15:09
     * @office: 智能控制设计科
     * @research institute： 智能控制设计所
     * @department： 机械设计院
     * @company: 辽宁忠旺机械设备制造有限公司
     * @group： 忠旺集团
     * @version: 1.0
     */
    public JsonResult loadTransOrderData(Integer id) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setType(JsonResult.ResultType.SUCCESS);
        Map<String,Object> data = new HashMap<>();
        TransOrderDataModel transOrderDataModel = transQueryMapper.loadTransOrderDataById(id);
        jsonResult.setData(transOrderDataModel);
        return jsonResult;
    }

    /***
     * 撤销已下料首序
     * @param transModel
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int flowRevoke(TransModel transModel) {
        int i = 0;
        Integer routeId = transQueryMapper.selFlowTransRouteId(transModel.getTransId());
        Integer sheetId = transQueryMapper.selFlowRouteSheetId(routeId);
        if (sheetId != null) {
            transQueryMapper.delFlowTransId(transModel.getTransId());
            transQueryMapper.updateFlowRouteId(routeId);
            i = transQueryMapper.updateFlowSheetCutStauts(sheetId);
        } else {
            return -1;
        }
        return i;
    }
}
